package com.lanluo.service.structure;

/**
 * 下压堆栈（链表的实现）
 * @author
 * @date 2018-08-27-14:15
 */
public class Stack<Item> {

    private Node first; //栈顶（最近添加元素）
    private int N;      //元素数量

    private class Node {
        Item item;
        Node next;
    }

    /**
     * 向栈顶添加元素
     * @param item
     */
    public void push(Item item) {
        Node oldFirst = first;
        first = new Node();
        first.item = item;
        first.next = oldFirst;
        N++;
    }

    /**
     * 从栈顶删除元素
     * @return
     */
    public Item pop(){
        Item item = first.item;
        first = first.next;
        N--;
        return item;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public int size() {
        return N;
    }

    public static void main(String[] args) {
        Stack<String> s = new Stack<String>();
        for (int i = 0; i < 10; i++) {
            s.push(i+"");
        }
        while (!s.isEmpty()) {
            System.out.println(s.pop());
        }

    }

}